package com.nd.mobile.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.domain.Bluebook;
import com.nd.mobile.study.domain.BluebookCatalog;
import com.nd.mobile.study.domain.Result;
import com.nd.mobile.study.view.helper.ToastHelper;

public class BlueBookInfoDetailActivity extends BaseActivity {
	
	private BluebookCatalog mCatalog;
	
	private Bluebook  mBluebook;
	//,mTvBBAnsContent
	private TextView mTvBBType,mTvBBIndex,mTvBBContent;
	
	private RadioGroup choiceRadio,yonRadio;
	
	private CheckBox checkBox_A,checkBox_B,checkBox_C,checkBox_D;
	
	private LinearLayout mLLChoices,checkboxarea;
	
	private Button mBtnSubmit;
		
	private String userAnswer;
	
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.bluebook_detail);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getIntentData();
		showDetail();
	}

	private void getIntentData() {
		mCatalog = (BluebookCatalog) getIntent().getSerializableExtra(BundleConstants.HOMEWORK_INFO);
	}
	private void showDetail() {
		mLoading.p();
		new GetDetailTask(this).execute();
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		mTvBBType = (TextView) findViewById(R.id.bluebook_type);
		mTvBBIndex = (TextView) findViewById(R.id.bluebook_index);
		mTvBBContent = (TextView) findViewById(R.id.bluebook_content);
		mLLChoices = (LinearLayout) findViewById(R.id.choices);
		mBtnSubmit = (Button) findViewById(R.id.bluebook_submit);
//		mTvBBAnsContent = (TextView) findViewById(R.id.bluebook_answerContent);
		choiceRadio = (RadioGroup) findViewById(R.id.radioGroup1);
		yonRadio = (RadioGroup) findViewById(R.id.radioGroup2);
		checkboxarea = (LinearLayout) findViewById(R.id.checkbox_area);
		checkBox_A = (CheckBox) findViewById(R.id.check0);
		checkBox_B = (CheckBox) findViewById(R.id.check1);
		checkBox_C = (CheckBox) findViewById(R.id.check2);
		checkBox_D = (CheckBox) findViewById(R.id.check3);
	}
	
	private class GetDetailTask extends SimpleAsyncTask<Void, Void, Result<Bluebook[]>> {

		Result<Bluebook[]> bluebooks = null;
		
		public GetDetailTask(Context context) {
			super(context);
		}

		@Override
		protected Result<Bluebook[]> onLoad(Void... params) throws Exception {
			try {
				bluebooks = mCatalog.getBlueBook(App.getInstance().getUser(),mBluebook);
			} catch (LocalErrorException e) {
				e.printStackTrace();
				ToastHelper.displayToast(BlueBookInfoDetailActivity.this, e.getLocalizedMessage());
			} catch (Exception e) {
				e.printStackTrace();
				ToastHelper.displayToast(BlueBookInfoDetailActivity.this, "数据异常");

			}
			return bluebooks;
		}

		@Override
		protected void onUI(Result<Bluebook[]> detail) {
//			if(mBluebook.getSerialNum()==mBluebook.getTotalNum()-1){
			if(mBluebook!=null&&mBluebook.getSerialNum()==mBluebook.getTotalNum()-1){
				//最后一题
				String content = detail.getPrompt();
				toFinishActivity(content);
			}else{
				if(detail!=null&&detail.getBody()!=null&&detail.getBody().length>0){
					mBluebook = detail.getBody()[0];
					showBluebook();
				}else{
					toFinishActivity("本组作业已完成");
				}
			}
			mLoading.v();
		}

	}
	private void toFinishActivity(String content){
		Intent mIntent = new Intent();
		mIntent.setClass(BlueBookInfoDetailActivity.this,FinishedActivity.class);
		mIntent.putExtra(BundleConstants.FINISH_INFO, content);
		startActivity(mIntent);
		finish();
	}
	public void showBluebook() {
		userAnswer = "";
		mTvBBType.setText("题型: "+mBluebook.getTypeName()+"("+mBluebook.getTotalNum()+")");
		mTvBBIndex.setText("序号: 第"+mBluebook.getPcID()+"题");
		mTvBBContent.setText(Html.fromHtml(mBluebook.getContentNote()));
//		mTvBBAnsContent.setText(Html.fromHtml(mBluebook.getAnswer()));
		if(Bluebook.CHOICE.equals(mBluebook.getProblemTypeID())){
			choiceRadio.setVisibility(View.VISIBLE);
			choiceRadio.check(-1);
			yonRadio.setVisibility(View.GONE);
			checkboxarea.setVisibility(View.GONE);
			//单选题
//			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//			RadioGroup radioGroup = new RadioGroup(this);
//			radioGroup.setLayoutParams(params);
//			radioGroup.setId(R.string.choice);
//			for(String answer : mBluebook.getAnswer()){
//				RadioButton radioBtn = new RadioButton(this);
//				radioBtn.setLayoutParams(params);
//			}
		}else if(Bluebook.YES_OR_NO.equals(mBluebook.getProblemTypeID())){
			//单选
			
			yonRadio.setVisibility(View.VISIBLE);
			choiceRadio.setVisibility(View.GONE);
			yonRadio.check(-1);
			checkboxarea.setVisibility(View.GONE);
		}else if(Bluebook.MULCHOICE.equals(mBluebook.getProblemTypeID())){
			//多选
			checkboxarea.setVisibility(View.VISIBLE);
			checkBox_A.setChecked(false);
			checkBox_B.setChecked(false);
			checkBox_C.setChecked(false);
			checkBox_D.setChecked(false);
			yonRadio.setVisibility(View.GONE);
			choiceRadio.setVisibility(View.GONE);
			
		}
		mLLChoices = (LinearLayout) findViewById(R.id.choices);
		mBtnSubmit = (Button) findViewById(R.id.bluebook_submit);
		mBtnSubmit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(Bluebook.CHOICE.equals(mBluebook.getProblemTypeID())){
					switch(choiceRadio.getCheckedRadioButtonId()){
					case R.id.radio_A:userAnswer="A";break;
					case R.id.radio_B:userAnswer="B";break;
					case R.id.radio_C:userAnswer="C";break;
					case R.id.radio_D:userAnswer="D";break;
					}
				}else if(Bluebook.YES_OR_NO.equals(mBluebook.getProblemTypeID())){
					//单选
					if(Bluebook.YES_OR_NO.equals(mBluebook.getProblemTypeID())){
						switch(yonRadio.getCheckedRadioButtonId()){
						case R.id.radio_Y:userAnswer="√";break;
						case R.id.radio_N:userAnswer="×";break;
						}
					}
				}else if(Bluebook.MULCHOICE.equals(mBluebook.getProblemTypeID())){
					//多选
					if(Bluebook.MULCHOICE.equals(mBluebook.getProblemTypeID())){
						if(checkBox_A.isChecked()){
							userAnswer = "A";
						}
						if(checkBox_B.isChecked()){
							userAnswer += "B";
						}
						if(checkBox_C.isChecked()){
							userAnswer += "C";
						}
						if(checkBox_D.isChecked()){
							userAnswer += "D";
						}
					}
					
				}
				mBluebook.setUserAnswer(userAnswer);
				new GetDetailTask(BlueBookInfoDetailActivity.this).execute();				
			}
		});
	}
}
